package com.fourth_groups.shopping121store.ui.person.view;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.fourth_groups.shopping121store.R;
import com.fourth_groups.shopping121store.bean.Person_horizontal;
import com.fourth_groups.shopping121store.bean.Person_line;
import com.fourth_groups.shopping121store.bean.Person_order;
import com.fourth_groups.shopping121store.bean.Person_user;
import com.fourth_groups.shopping121store.bean.Person_verticle;
import com.fourth_groups.shopping121store.bean.Person_verticle1;
import com.fourth_groups.shopping121store.http.OkHttpClientUtils;
import com.fourth_groups.shopping121store.ui.person.adapter.Grad_meadapter;
import com.fourth_groups.shopping121store.ui.person.adapter.Grad_payadapter;
import com.fourth_groups.shopping121store.ui.person.adapter.Listadapter;
import com.fourth_groups.shopping121store.ui.person.utils.GlideCircleTransform;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * ----------Dragon be here!----------/
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃  创建人:李培雄
 * 　　┃　┳┛　┗┳　┃  时间:Administrator on 2016/10/10
 * 　　┃　　　　　　　┃  备注:我
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃代码无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━by:coder-pig
 */
public class PersonFragment extends Fragment {


    public static GridView grad_pay;
    public GridView grad_me;
    public ListView lv_adress;
    private String json;
    private List<Person_order.ItemsBean> list3;
    private Message msg;
    private List<Person_user.ItemsBean> user;
    private List<Person_line.ItemsBean> line;
    private List<Person_order.ItemsBean> order;
    private List<Person_horizontal> horizontal;
    private List<Person_verticle> verticle;
    private List<Person_verticle1> verticle1List;
    private Message msg1;
    private Message msg2;
    private Message msg3;
    private Message msg4;
    private Message msg5;
    private Grad_payadapter adapter;
    private ImageView image_title;
    private TextView text_title;
    private ImageButton title_ivb;
    private ImageButton image_set;
    public PersonFragment() {
        // Required empty public constructor
    }
    public Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            //接收数据进行判断
           if (msg.what==1){
           }
           else if (msg2.what==3){
               adapter = new Grad_payadapter(getContext());
               adapter.setData((List<Person_order.ItemsBean>) msg.obj);
               grad_pay.setAdapter(adapter);

           }else if (msg.what==4){
               Grad_meadapter meadapter=new Grad_meadapter(getContext());
               meadapter.setData((List<Person_horizontal>) msg3.obj);
                grad_me.setAdapter(meadapter);

           }
            else if (msg.what==5){
               Listadapter listadapter=new Listadapter(getContext());
               listadapter.setData((List<Person_verticle>) msg4.obj);
               lv_adress.setAdapter(listadapter);

          }
        }
    };
    private static PersonFragment cartFragment;
    public  static Fragment setFragment(){
        if (cartFragment==null)
            synchronized (PersonFragment.class){
                if (cartFragment==null){
                    cartFragment=new PersonFragment();
                }
            }
        return cartFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_person, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        grad_pay = (GridView) view.findViewById(R.id.grad_pay);
        grad_me = (GridView) view.findViewById(R.id.grad_me);
        lv_adress = (ListView) view.findViewById(R.id.lv_adress);
        image_title = (ImageView) view.findViewById(R.id.image_title);
        text_title = (TextView) view.findViewById(R.id.text_title);
        title_ivb = (ImageButton) view.findViewById(R.id.title_ivb);
        image_set = (ImageButton)view. findViewById(R.id.ivg_setting);
        Glide.with(getContext()).load("http://cdn.static.121dian.com/front/assets/img/app/default_user_avatar/4.jpg").placeholder(R.drawable.personal_default_icon).transform(new GlideCircleTransform(getContext())).into(image_title);
        image_set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent3=new Intent(getActivity(),SettingActivity.class);
                startActivity(intent3);
            }
        });
        title_ivb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(),PeopleActivity.class);
                startActivity(intent);
            }
        });
        Gragmelisten();
        OkHttpClientUtils.getDataAsync(getActivity().getApplicationContext(), "http://api.121dian.cn/v3/custommenu?access_token=bi2Qn6Oi8KJguHAebOaO", new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
            }
            //下载解析
            @Override
            public void onResponse(Response response) throws IOException {
                if (response.isSuccessful()){

                    json = response.body().string();

                                try {
                                    JSONObject json1=new JSONObject(json);
                                    msg = Message.obtain();
                                    msg1 = Message.obtain();
                                    msg2 = Message.obtain();
                                    msg3 = Message.obtain();
                                    msg4 = Message.obtain();
                                    msg5 = Message.obtain();
                                    JSONObject data = json1.getJSONObject("data");
                                    JSONArray block = data.getJSONArray("block");
                                    user = new ArrayList<>();
                                    line = new ArrayList<>();
                                    order = new ArrayList<>();
                                    horizontal = new ArrayList<>();
                                    verticle = new ArrayList<>();
                                    verticle1List = new ArrayList<>();
                                    for (int i=0;i<block.length();i++){

                                        JSONObject obj = block.getJSONObject(i);
                                        String type = obj.getString("type");
                                        if (("user-info").equals(obj.getString("type"))) {

                                            JSONArray items = obj.getJSONArray("items");
                                            for (int j = 0; j < items.length(); j++) {
                                                JSONObject jsonObject = items.getJSONObject(j);
                                                String nickname = jsonObject.getString("nickname");
                                                String avatar = jsonObject.getString("avatar");
                                                Person_user.ItemsBean user1 = new Person_user.ItemsBean(nickname, avatar);
                                                user.add(user1);
                                            }
                                            msg.obj = user;
                                            msg.what = 1;

                                        }else if (obj.getString("type").equals("line")){
                                            JSONArray item=obj.getJSONArray("items");
                                            for (int m=0;m<item.length();m++){
                                                JSONObject jsonObject = item.getJSONObject(m);
                                                String color = jsonObject.getString("color");
                                                int height = jsonObject.getInt("height");
                                                Person_line.ItemsBean line1=new Person_line.ItemsBean(color,height);
                                                line.add(line1);
                                            }
                                            msg1.obj= line;
                                            msg1.what=2;

                                        }else
                                        if (obj.getString("type").equals("order-info")){

                                            JSONArray item1=obj.getJSONArray("items");
                                            for (int s=0;s<item1.length();s++){
                                                JSONObject jsonObject1 = item1.getJSONObject(s);
                                                String icon = jsonObject1.getString("icon");
                                                String name = jsonObject1.getString("name");
                                                String url = jsonObject1.getString("url");
                                                Person_order.ItemsBean order1=new Person_order.ItemsBean(name,icon,url);
                                                order.add(order1);
                                            }
                                            msg2.what=3;
                                            msg2.obj= order;

                                        }
                                        else if (obj.getString("type").equals("horizontal-list")) {

                                            JSONArray item2 = obj.getJSONArray("items");
                                            for (int a = 0; a < item2.length(); a++) {
                                                JSONObject jsonObject2 = item2.getJSONObject(a);
                                                int id = jsonObject2.getInt("id");
                                                //String name = jsonObject2.getString("name");
                                                String icon = jsonObject2.getString("icon");
                                                String url = jsonObject2.getString("url");
                                                String title = jsonObject2.getString("title");
                                                Person_horizontal horizontal1 = new Person_horizontal(id, icon, url, title);
                                                horizontal.add(horizontal1);
                                            }
                                            msg3.obj = horizontal;
                                            msg3.what = 4;

                                        }else if (obj.getString("type").equals("verticle-list")){

                                            JSONArray item3=obj.getJSONArray("items");

                                            for (int b=0;b<item3.length();b++){
                                                JSONObject jsonObject3 = item3.getJSONObject(b);
                                                int id = jsonObject3.getInt("id");
                                                String icon = jsonObject3.getString("icon");
                                                String url = jsonObject3.getString("url");
                                                String title = jsonObject3.getString("title");
                                                Person_verticle verticle1=new Person_verticle(id,icon,url,title);
                                                verticle.add(verticle1);

                                            }
                                            msg4.obj= verticle;
                                            msg4.what=5;
                                        }
                                    }
                                    handler.sendMessage(msg);
                                    handler.sendMessage(msg1);
                                    handler.sendMessage(msg2);
                                    handler.sendMessage(msg3);
                                    handler.sendMessage(msg4);
                                    handler.sendMessage(msg5);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                }
            }
        }, new Object());
    }

        //点击事件
        private void Gragmelisten(){
            grad_pay.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Person_order.ItemsBean itemsbean= (Person_order.ItemsBean) grad_pay.getItemAtPosition(position);
                    Intent intent1 =new Intent(getContext(),Gradpay.class);
                    intent1.putExtra("url",itemsbean.getUrl());
                    startActivity(intent1);
                }
            });
            grad_me.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Person_horizontal horizontal=(Person_horizontal) grad_me.getItemAtPosition(position);
                        Intent intent=new Intent(getActivity(),GradmeActivity.class);
                        intent.putExtra("url",horizontal.getUrl());
                        intent.putExtra("id",horizontal.getId());
                        Log.i("t",""+horizontal.getId());
                        Log.i("t",horizontal.getUrl());
                         startActivity(intent);
                    }
            });
    lv_adress.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Person_verticle verticle= (Person_verticle) lv_adress.getItemAtPosition(position);
            Intent intent2=new Intent(getActivity(),LvActivity.class);
            intent2.putExtra("url",verticle.getUrl());
            startActivity(intent2);
        }
    });
        }
}
